import Router from "@koa/router";
import z from "zod";

import { DishController } from "../controllers/dishes.controller";
import { AuthUtil } from "../utils/auth.util";
import { validateBody, validateParams } from "../middleware/validation";
import {
  CreateDishesRequestSchema,
  UpdateDishesRequestSchema,
} from "../validators/dishes.validator";
import { categoryIdSchema, idSchema } from "../validators/request.validator";

const dishesRouter = new Router({
  prefix: "/api",
});

// 获取所有菜品
dishesRouter.get("/dishes", DishController.listAll);

// 获取单个菜品
dishesRouter.get(
  "/dishes/:id",
  validateParams(idSchema),
  DishController.getDishById
);

// 新增菜品
dishesRouter.post(
  "/dishes",
  AuthUtil.isAdmin(),
  validateBody(CreateDishesRequestSchema),
  DishController.createDish
);

// 更新菜品
dishesRouter.put(
  "/dishes/:id",
  AuthUtil.isAdmin(),
  validateParams(idSchema),
  validateBody(UpdateDishesRequestSchema),
  DishController.updateDish
);

// 删除菜品
dishesRouter.delete(
  "/dishes/:id",
  AuthUtil.isAdmin(),
  validateParams(idSchema),
  DishController.deleteDish
);

// 根据菜品分类id查询菜品
dishesRouter.get(
  "/categories/:categoryId/dishes",
  validateParams(categoryIdSchema),
  DishController.getDishesByCategoryId
);

// 菜品评分（普通用户权限）
dishesRouter.post(
  "/dishes/:id/ratings",
  AuthUtil.isNormal(),
  validateParams(idSchema),
  validateBody(z.object({ score: z.number().int().min(1).max(10) })),
  DishController.rateDish
);

export default dishesRouter;
